
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EtchedBorder;

public class GUI extends JPanel {

    private static final String DATE_FORMAT_NOW = "HH:mm:ss";
    private JTextArea chat = new JTextArea();
    private JTextArea clients = new JTextArea("Clients\n");
    private JTextField chatInput = new JTextField();
    private JButton send = new JButton("Send!");

    public GUI() {
        JFrame main = new JFrame("ChatMaster 2000");
        main.setSize(720, 480);
        main.setDefaultCloseOperation(main.EXIT_ON_CLOSE);
        main.setResizable(false);
        main.getContentPane().setLayout(new BorderLayout());

        chat.setEditable(false);

        clients.setEditable(false);
        clients.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));

        send.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
                addLineToChat(chatInput.getText());
                chatInput.setText("");
            }
        });

        chatInput.addKeyListener(new KeyListener() {

            public void keyPressed(KeyEvent e) {
                if (e.getKeyChar() == e.VK_ENTER) {
                    addLineToChat(chatInput.getText());
                    chatInput.setText("");
                }
            }

            // Ubrukte abstrakte metoder
            public void keyTyped(KeyEvent e) {
            }

            public void keyReleased(KeyEvent e) {
            }
        });

        // Test av klientfelt
        addClient("Tore Berg Hansen");
        addClient("Greta Hjertø");

        // Test av chatfelt
        addLineToChat("Drittstøvel!");

        JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, false, chatInput, send);
        split.setDividerLocation(650);
        split.setOneTouchExpandable(false);

        main.getContentPane().add(chat, BorderLayout.CENTER);
        main.getContentPane().add(clients, BorderLayout.EAST);
        main.getContentPane().add(split, BorderLayout.SOUTH);

        main.setVisible(true);
        chatInput.requestFocusInWindow();
    }

    public void addClient(String client) {
        if (!client.equals("")) {
            clients.append(client + "\n");
        }
    }

    public void addLineToChat(String msg) {
        if (!msg.trim().equals("")) {
            chat.append(currentTime() + "  You > " + msg +"\n");
        }
    }

    public static String currentTime() {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
        return sdf.format(cal.getTime());
    }
}
